from django.shortcuts import render

# Create your views here.

from medicine.models import MedicineDirectionModel, MedicineClassificationModel, MedicineModel, MedicineContentModel
from medicine.ser import MedicineDirectionModelSerializer, MedicineClassificationModelSerializer, MedicineModelSerializer, MedicineContentModelSerializer

# from rest_framework.permissions import AllowAny

from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.filters import OrderingFilter

# 分页配置
class StandardResultsSetPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    max_page_size = 100

# 排序配置
class CustomOrderingFilter(OrderingFilter):
    ordering_params = ('direction', 'classification', 'name')

# 药物方向 ListAPIView
class MedicineDirectionListView(ListAPIView):
    serializer_class = MedicineDirectionModelSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['direction']

    def get_queryset(self):
        return MedicineDirectionModel.objects.all()

# 药物分类 ListAPIView
class MedicineClassificationListView(ListAPIView):
    serializer_class = MedicineClassificationModelSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['classification']

    def get_queryset(self):
        return MedicineClassificationModel.objects.all()

# 药物 ListAPIView
class MedicineListView(ListAPIView):
    serializer_class = MedicineModelSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['name']

    def get_queryset(self):
        return MedicineModel.objects.all()

# 药物内容 ListAPIView
class MedicineContentListView(ListAPIView):
    serializer_class = MedicineContentModelSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['content']

    def get_queryset(self):
        return MedicineContentModel.objects.all()